/**
 * 
 */
package wpmp.security.mgr.pub;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;

import wcommons.beanutils.BeanUtils;
import wpmp.security.mgr.Statics;
import wpmp.security.mgr.dao.OrgDao;
import wpmp.utils.quickbean.Org;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 11:43:52 AM Dec 5, 2013
 */
@Component
@Lazy(false)
public class OrgUtils {

	private static final Logger LOG = LoggerFactory.getLogger(OrgUtils.class);

	private static OrgDao dao;

	private static Map<Long, Org> VALUES;

	@Autowired
	public void setDao(OrgDao dao) {
		OrgUtils.dao = dao;
	}

	@PostConstruct
	public void init() {
		if (LOG.isDebugEnabled()) {
			LOG.debug("start to init {}", getClass().getSimpleName());
		}

		OrgUtils.VALUES = load();

		if (LOG.isDebugEnabled()) {
			LOG.debug("success to init {}", getClass().getSimpleName());
		}
	}

	private Map<Long, Org> load() {
		final Map<Long, Org> results = new HashMap<Long, Org>();
		final SqlRowSet rs = dao.queryForRowSet();
		while (rs.next()) {
			final Org org = new Org();
			BeanUtils.copySqlRowSetToBean(org, rs);
			results.put(rs.getLong("id"), org);
		}
		return results;
	}

	public static Org get(Long id) {
		Org org = getFromCache(id);
		if (org == null) {
			org = getFromDB(id);
			if (org != null) {
				VALUES.put(id, org);
			}
		}
		return org;
	}

	public static Org getFromCache(Long id) {
		return VALUES.get(id);
	}

	public static Org getFromDB(Long id) {
		final SqlRowSet rs = dao.queryForRowSet(id);
		if (!rs.next()) {
			return null;
		}
		final Org result = new Org();
		BeanUtils.copySqlRowSetToBean(result, rs);
		return result;
	}

	public static void remove(Long id) {
		VALUES.remove(id);
	}

	public static String getOrgName(Long id) {
		Org org = get(id);
		return org != null ? org.getName() : null;
	}

	public static String getOrgName(Long id, String defaultValue) {
		Org org = get(id);
		return org != null ? org.getName() : defaultValue;
	}

	public static String getOrgNameDefaultUngrouped(Long id) {
		Org org = get(id);
		if (org == null) {
			org = getUngrouped();
		}
		return org != null ? org.getName() : Statics.UNGROUPED_NAME;
	}

	public static Org getRoot() {
		return get(Statics.ROOT_ID);
	}

	public static Org getUngrouped() {
		return get(Statics.UNGROUPED_ID);
	}

	@PreDestroy
	public void destroy() {
		if (LOG.isDebugEnabled()) {
			LOG.debug("start to destroy {}", getClass().getSimpleName());
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("success to destroy {}", getClass().getSimpleName());
		}
	}
}
